<script type="text/javascript" src="/js/jquery.ajaxfileupload.js"></script>
<h1 class="page_title">VB作业添加</h1>
<div class="panel panel-default">
    <div class="panel-header">
        作业添加
    </div>
    <div class="panel-body">
        <form class="form form-horizontal responsive" id="demoform" novalidate="novalidate">
            <div class="row cl">
                <label class="form-label col-xs-3">作业名称：</label>
                <div class="formControls col-xs-8">
                    <input type="text" class="input-text valid" placeholder="例：1-13自测题" id="projectName"
                           autocomplete="off">
                </div>
            </div>
            <div class="row cl">
                <label class="form-label col-xs-3">作业描述：</label>
                <div class="formControls col-xs-8">
                    <textarea class="textarea valid" placeholder="例：VB项目包含.vbp.frm.frx.bas文件，需压缩为zip文件上传"
                              id="expression"
                              autocomplete="off"></textarea>
                </div>
            </div>
            <div class="row cl">
                <label class="form-label col-xs-3">作业要求word上传：</label>
                <#--                带文本框-->
                <div class="formControls col-xs-8">
                <span class="btn-upload form-group">
                    <input class="input-text upload-url" type="text" name="docFileLoc" id="docFileLoc" readonly
                           placeholder="/Users/StupidZhe/IT/VB-T/VB03-T/VB03.doc"><a
                            href="javascript:void();" class="btn btn-primary"><i class="Hui-iconfont">&#xe6dc;</i> 浏览doc</a>
                    <input type="file" name="docFile" class="input-file" id="docFile">
                </span>
                </div>
            </div>
            <div class="row cl">
                <label class="form-label col-xs-3">作业要求上传：</label>
                <#--                带文本框-->
                <div class="formControls col-xs-8">
                <span class="btn-upload form-group">
                    <input class="input-text upload-url" type="text" name="demo" id="demo" readonly
                           placeholder="/Users/StupidZhe/IT/VB-T/VB03-T/prg1"><a
                            href="javascript:void();" class="btn btn-primary"><i class="Hui-iconfont">&#xe6dc;</i> 浏览文件</a>
                    <input type="file" name="projectFile" class="input-file" id="projectFile">
                </span>
                </div>
            </div>
            <div class="row cl">
                <label class="form-label col-xs-3">作业类型：</label>
                <div class="mt-20 skin-minimal">
                    <div class="radio-box">
                        <input type="radio" id="radio-1" name="classification" checked value="0">
                        <label for="radio-1">实例题</label>
                    </div>
                    <div class="radio-box">
                        <input type="radio" id="radio-2" name="classification" value="1">
                        <label for="radio-2">设计题</label>
                    </div>
                </div>
            </div>
            <div class="row cl">
                <label class="form-label col-xs-3">学生最大上传次数：</label>
                <div class="formControls col-xs-8">
                    <input type="number" class="input-text valid" value="3" id="upTimesMax"
                           autocomplete="off">
                </div>
            </div>
            <div class="row cl">
                <div class="col-xs-8 col-xs-offset-3">
                    <input id="addProjectBtn" class="btn btn-primary" type="button" value="&nbsp;&nbsp;添加&nbsp;&nbsp;">
                    <input class="btn btn-danger" type="reset" value="&nbsp;&nbsp;重置&nbsp;&nbsp;">
                </div>
            </div>
        </form>
    </div>
</div>
<script type="text/javascript">
    function isNumber(obj) {
        return typeof obj === 'number' && isFinite(obj)
    }

    var docLocation = null;
    var demoLocation = null;

    $("#addProjectBtn").on("click", function () {
        // stupidAjaxGetJson("")
    });
    $('#docFile').ajaxfileupload({
        action: '/adm/project/uploadDoc',
        valid_extensions: ['doc', 'docx', 'pdf', 'txt'],
        // valid_extensions: ['doc', 'docx','pdf','txt'],
        onStart: function () {
            layer.msg("正在上传中，请稍等", {icon: 4, shade: 0.5,time: 0});
            $("#docFileLoc").val($('#docFile').val());
        },
        onComplete: function (data) {
            setTimeout(function () {
                layer.closeAll();
                if (data.success) {
                    docLocation = data.msg;
                    layer.msg("上传成功", {time: 2000, icon: 1, shadeClose: true, shade: 0.5});
                } else {
                    if (data.msg !== undefined) {
                        layer.msg("上传失败：" + data.msg, {time: 0, shadeClose: true, icon: 2, shade: 0.5});
                    } else {
                        layer.msg("上传失败：不支持所上传的文档格式", {time: 0, shadeClose: true, icon: 2, shade: 0.5});
                    }
                }
            },1000);
        }
    });
    $('#projectFile').ajaxfileupload({
        action: '/adm/project/uploadProject',
        valid_extensions: ['zip'],
        // valid_extensions: ['doc', 'docx','pdf','txt'],
        onStart: function () {
            layer.msg("作业上传中，请稍等（由于需要进行编译，等待时间会因源程序差异而不同）", {icon: 4, shade: 0.5,time: 0});
            $("#demo").val($('#projectFile').val());
        },
        onComplete: function (data) {
            setTimeout(function () {
                layer.closeAll();
                if (data.success) {
                    demoLocation = data.msg;
                    layer.msg("上传成功", {time: 2000, icon: 1, shadeClose: true, shade: 0.5});
                } else {
                    $("#projectFile").val('');
                    if (data.msg !== undefined) {
                        layer.msg("上传失败：" + data.msg, {time: 0, shadeClose: true, icon: 2, shade: 0.5});
                    } else {
                        layer.msg("上传失败：仅支持zip文件", {time: 0, shadeClose: true, icon: 2, shade: 0.5});
                    }
                }
            }, 1000);
        }
    });
    $("#addProjectBtn").on("click", function () {
        var projectName = $("#projectName").val();
        var expression = $("#expression").val();
        var upTimesMax = $("#upTimesMax").val();
        var classification = $("input[name='classification']:checked").val();
        if (projectName.trim() === '') {
            alert("作业名称不能为空");
            return;
        }
        if (demoLocation === null) {
            alert("作业样例不能为空");
            return;
        }
        stupidAjaxGetJson("/adm/project/add",
            "POST",
            {
                projectName: projectName,
                expression: expression,
                classification: classification,
                demoLocation: demoLocation,
                docLocation: docLocation,
                upTimesMax: upTimesMax
            },
            function (data) {
                alert(data.msg);
                if (data.success) {
                    selectView("/adm/project/", "作业管理");
                }
            });
    });
</script>